﻿
Partial Class Article_Show
    Inherits System.Web.UI.Page
    Function ReplaceKey(ByVal content)
        'On Error Resume Next
        Dim conn, rs
        conn = Server.CreateObject("ADODB.Connection")
        conn.open(ConfigurationSettings.AppSettings("SQLConnString") & """" & Server.MapPath(".") & "\" & ConfigurationSettings.AppSettings("dbPath") & """")
        Dim oReplace
        rs = Server.CreateObject("adodb.recordset")
        rs.open("select  * from Article_Key order by [Order] desc", conn, 1, 3)

        While Not rs.eof

            oReplace = rs("Replace").value
            If IsDBNull(oReplace) Then oReplace = 0
            If oReplace = 0 Then oReplace = -1
            content = p_replace(content, rs("keyword").value, "<a href=""" & rs("Url").value & """ target=""_blank""><font color=blue>" & rs("keyword").value & "</font></a>", 1, oReplace, 1)
            rs.movenext()
        End While
        rs.close()
        conn.close()
        ReplaceKey = content
    End Function

    Function p_replace(ByVal content, ByVal asp, ByVal htm, ByVal aa, ByVal Rnum, ByVal bb)
        Dim objRegExp, strs, p
        strs = content
        objRegExp = New Regex("(\<a[^<>]+\>.+?\<\/a\>)|(\<img[^<>]+\>)", RegexOptions.IgnoreCase)
        Dim Matches As MatchCollection = objRegExp.Matches(strs)
        p = 0
        Dim MyArray()
        For i As Integer = 0 To Matches.Count - 1
            ReDim Preserve MyArray(i)
            MyArray(i) = Mid(Matches(i).Value, 1, len(Matches(i).Value))
            strs = replace(strs, Matches(i).Value, "<" & i & ">", 1, Rnum, 1)
            p = p + 1
        Next
        If p = 0 Then
            content = replace(content, asp, htm, 1, Rnum, 1)
            p_replace = content
            Exit Function
        End If
        strs = replace(strs, asp, htm, 1, Rnum, 1)
        For p = 0 To ubound(MyArray)
            strs = replace(strs, "<" & p & ">", MyArray(p), 1, Rnum, 1)
        Next
        p_replace = strs
    End Function
End Class
